package com.ptmall.app.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import com.ptmall.app.R;
import com.ptmall.app.base.BaseMvpActivity;
import com.ptmall.app.bean.AddressBean;
import com.ptmall.app.bean.CollectionBean;
import com.ptmall.app.bean.PageData;
import com.ptmall.app.net.api.ApiDataRepository;
import com.ptmall.app.net.base.ApiNetResponse;
import com.ptmall.app.ui.adapter.AddressAdapter;
import com.ptmall.app.ui.adapter.CollectionAdapter;
import com.ptmall.app.view.PullToRefreshLayout;
import com.ptmall.app.view.PullableListView;
import com.ptmall.app.view.TitleBarView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AddressActivity extends BaseMvpActivity {

    TitleBarView titlebar;
    PullableListView mListview;
    PullToRefreshLayout mRefreshView;

    ApiDataRepository apiDataRepository = ApiDataRepository.getInstance();

    List<AddressBean> mDataLists = new ArrayList<>();
    int page = 1;
    AddressAdapter mAddressAdapter;

    boolean select;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setView(R.layout.activity_mangeaddress);
    }

    @Override
    public void initViews() {
        select = getIntent().getBooleanExtra("select", false);
        titlebar = (TitleBarView) findViewById(R.id.titlebar);


        titlebar.setOnTitleBarClickListener(new TitleBarView.TitleBarClickListener() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {
                startActivity(new Intent(getContext(), AddAddressActivity.class));
            }
        });

        mListview = (PullableListView) findViewById(R.id.listview);
        mRefreshView = (PullToRefreshLayout) findViewById(R.id.refresh_view);

        mRefreshView.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                page = 1;
                getData();
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                page++;
                getData();
            }
        });

        mListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                if (select) {
//                    Intent intent = new Intent();
//                    // 获取用户计算后的结果
//                    intent.putExtra("name", mDataLists.get(i).trueName); //将计算的值回传回去
//                    intent.putExtra("phone", mDataLists.get(i).tel); //将计算的值回传回去
//                    intent.putExtra("address", mDataLists.get(i).area+mDataLists.get(i).address); //将计算的值回传回去
//                    intent.putExtra("id", mDataLists.get(i).addressId); //将计算的值回传回去
//                    //通过intent对象返回结果，必须要调用一个setResult方法，
//                    //setResult(resultCode, data);第一个参数表示结果返回码，一般只要大于1就可以，但是
//                    setResult(1, intent);
//
//                    finish(); //结束当前的activity的生命周期
//                }else {
                    Intent intent = new Intent(getContext(),AddAddressActivity.class);
                    intent.putExtra("bean", (Serializable) mDataLists.get(i));
                    startActivity(intent);
//                }
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        mRefreshView.autoRefresh();
    }

    @Override
    public void initEvents() {

        mAddressAdapter = new AddressAdapter(getContext(), null);
        mListview.setAdapter(mAddressAdapter);

        mAddressAdapter.setClick(new AddressAdapter.Click() {
            @Override
            public void Click(int i) {
                if (select) {
                    Intent intent = new Intent();
                    // 获取用户计算后的结果
                    intent.putExtra("name", mDataLists.get(i).trueName); //将计算的值回传回去
                    intent.putExtra("phone", mDataLists.get(i).tel); //将计算的值回传回去
                    intent.putExtra("address", mDataLists.get(i).area+mDataLists.get(i).name+mDataLists.get(i).address); //将计算的值回传回去
                    intent.putExtra("id", mDataLists.get(i).addressId); //将计算的值回传回去
                    intent.putExtra("quname", mDataLists.get(i).name); //将计算的值回传回去
                    //通过intent对象返回结果，必须要调用一个setResult方法，
                    setResult(1, intent);

                    finish(); //结束当前的activity的生命周期
                }
            }

            @Override
            public void Click2(int i) {
                Intent intent = new Intent(getContext(),AddAddressActivity.class);
                intent.putExtra("bean", (Serializable) mDataLists.get(i));
                startActivity(intent);
            }
        });
    }


    private void getData() {
        Map<String, String> map = new HashMap<>();
        map.put("page", page + "");
        apiDataRepository.getAddressList(map, new ApiNetResponse<List<AddressBean>>() {
            @Override
            public void onSuccess(List<AddressBean> datas) {
                if (null != mRefreshView) {
                    mRefreshView.refreshFinish(PullToRefreshLayout.SUCCEED);
                    mRefreshView.loadmoreFinish(PullToRefreshLayout.SUCCEED);
                }
                if (page == 1) {
                    mDataLists.clear();
                    if (null != datas && datas.size() > 0) {
                        mDataLists.addAll(datas);
                    } else {
                    }
                } else {
                    if (null != datas && datas.size() > 0) {
                        mDataLists.addAll(datas);
                    } else {
                        showMsg(R.string.has_no_more);
                    }
                }
                mAddressAdapter.pushData(mDataLists);
            }
        });
    }
}
